package com.xwaf.platform.system.provider.config;

import org.quartz.Scheduler;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.config.PropertiesFactoryBean;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.core.io.ClassPathResource;
import org.springframework.scheduling.concurrent.ThreadPoolTaskExecutor;
import org.springframework.scheduling.quartz.SchedulerFactoryBean;

import javax.sql.DataSource;
import java.io.IOException;
import java.util.Properties;
import java.util.concurrent.Executor;

/**
 * 
 * <p>
 * Spring配置类 Schedule:<br>
 * 调度器 Trigger：触发器 job：任务（一个任务可以对应多个触发器）
 * <p>
 * 
 * @author 李聪 <br>
 * @email xwaf_love@yeah.net <br>
 * @since JDK 1.8<br>
 * @date 2019年12月27日 下午5:03:49 <br>
 * @see 无<br>
 *      Copyright (c) 2019, xwaf_love@yeah.net All Rights Reserved.<br>
 */
@Configuration
public class SchedulerConfig {

	@Autowired
	private DataSource dataSource;

	/**
	 * 
	 * <p>
	 * 调度器
	 * <p>
	 * 
	 * @author 李聪 <br>
	 * @email xwaf_love@yeah.net <br>
	 * @since JDK 1.8<br>
	 * @date 2019年12月27日 下午5:04:04 <br>
	 * @see 无<br>
	 * @return
	 * @throws Exception
	 * @demo <br>
	 */
	@Bean
	public Scheduler scheduler() throws Exception {
		Scheduler scheduler = schedulerFactoryBean().getScheduler();
		return scheduler;
	}

	/**
	 * 
	 * <p>
	 * Scheduler工厂类
	 * <p>
	 * 
	 * @author 李聪 <br>
	 * @email xwaf_love@yeah.net <br>
	 * @since JDK 1.8<br>
	 * @date 2019年12月27日 下午5:04:16 <br>
	 * @see 无<br>
	 * @return
	 * @throws IOException
	 * @demo <br>
	 */
	@Bean
	public SchedulerFactoryBean schedulerFactoryBean() throws IOException {
		SchedulerFactoryBean factory = new SchedulerFactoryBean();
		factory.setSchedulerName("Cluster_Scheduler");
		factory.setDataSource(dataSource);
		factory.setApplicationContextSchedulerContextKey("applicationContext");
		factory.setTaskExecutor(schedulerThreadPool());
		factory.setQuartzProperties(quartzProperties());
		factory.setStartupDelay(10);// 延迟10s执行
		return factory;
	}

	/**
	 * 
	 * <p>
	 * 加载配置属性
	 * <p>
	 * 
	 * @author 李聪 <br>
	 * @email xwaf_love@yeah.net <br>
	 * @since JDK 1.8<br>
	 * @date 2019年12月27日 下午5:04:33 <br>
	 * @see 无<br>
	 * @return
	 * @throws IOException
	 * @demo <br>
	 */
	@Bean
	public Properties quartzProperties() throws IOException {
		PropertiesFactoryBean propertiesFactoryBean = new PropertiesFactoryBean();
		propertiesFactoryBean.setLocation(new ClassPathResource("/spring-quartz.properties"));
		// 在quartz.properties中的属性被读取并注入后再初始化对象
		propertiesFactoryBean.afterPropertiesSet();
		return propertiesFactoryBean.getObject();
	}

	/**
	 * 
	 * <p>
	 * schedule配置线程池
	 * <p>
	 * 
	 * @author 李聪 <br>
	 * @email xwaf_love@yeah.net <br>
	 * @since JDK 1.8<br>
	 * @date 2019年12月27日 下午5:04:42 <br>
	 * @see 无<br>
	 * @return
	 * @demo <br>
	 */
	@Bean
	public Executor schedulerThreadPool() {
		ThreadPoolTaskExecutor executor = new ThreadPoolTaskExecutor();
		executor.setCorePoolSize(Runtime.getRuntime().availableProcessors());
		executor.setMaxPoolSize(Runtime.getRuntime().availableProcessors());
		executor.setQueueCapacity(Runtime.getRuntime().availableProcessors());
		return executor;
	}
}
